//
//  CXSegmentedBarView.m
//  CXCollege
//
//  Created by yangbaolong on 16/1/19.
//  Copyright © 2016年 北京畅想未来科技有限公司 网址：http://cxwlbj.com. All rights reserved.
//

#import "CXSegmentedBarView.h"

@implementation CXSegmentedBarView
#pragma - 初始化方法
- (void)awakeFromNib
{
    // 设置默认参数
    [self _initProperty];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        // 设置默认参数
        [self _initProperty];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // 设置默认参数
        [self _initProperty];
    }
    return self;
}

// 设置默认参数
- (void)_initProperty
{
    // 设置默认颜色
    self.backgroundColor = [UIColor whiteColor];
    self.titleColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.5];
    self.selectedTitleColor = kColor_navigationBar_color;
    self.selectedViewbgColor = kColor_navigationBar_color;
}

#pragma mark 设置视图颜色 setter
- (void)setTitleColor:(UIColor *)titleColor
{
    if (_titleColor != titleColor) {
        _titleColor = titleColor;
        
        // 当前是否有文本
        for (int i = 0; i < _titleLabels.count; i++) {
            // 当前文本不是选中文本
            if (i != _selectedIndex) {
                // 设置文本默认选中颜色
                UILabel *titleLabel = _titleLabels[i];
                titleLabel.textColor = _titleColor;
            }
        }
    }
}

- (void)setSelectedTitleColor:(UIColor *)selectedTitleColor
{
    if (_selectedTitleColor != selectedTitleColor) {
        _selectedTitleColor = selectedTitleColor;
        
        // 设置选中文本的颜色
        if (_titleLabels.count > _selectedIndex) {
            // 设置选中文本的标题颜色
            UILabel *selectedLabel = _titleLabels[_selectedIndex];
            selectedLabel.textColor = _selectedTitleColor;
        }
    }
}

- (void)setSelectedViewbgColor:(UIColor *)selectedViewbgColor
{
    if (_selectedViewbgColor != selectedViewbgColor) {
        _selectedViewbgColor = selectedViewbgColor;
        
        // 如果背景视图已经创建
        if (_selectedView != nil) {
            _selectedView.backgroundColor = _selectedViewbgColor;
        }
    }
}

#pragma mark - titles setter
- (void)setTitles:(NSArray *)titles
{
    if (_titles != titles) {
        _titles = titles;
        
        // 1.移除之前所有的文本视图
        for (UILabel *titleLabel in _titleLabels) {
            [titleLabel removeFromSuperview];
        }
        
        // 2.根据标题创建对应的文本视图
        // 文本视图的宽度
        float label_width = self.frame.size.width / titles.count;
        NSMutableArray *titleLabels = [NSMutableArray array];
        for (int i = 0; i < titles.count; i++) {
            // 创建文本视图
            UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(i * label_width, 0, label_width, self.frame.size.height - 0)];
            titleLabel.textAlignment = NSTextAlignmentCenter;
            titleLabel.font = [UIFont systemFontOfSize:16];
            titleLabel.textColor = _titleColor;
            titleLabel.text = titles[i];
            [self addSubview:titleLabel];
            // 添加到数组中
            [titleLabels addObject:titleLabel];
        }
        // 保存到全局变量中
        _titleLabels = titleLabels;
        
        // 3.创建选中文本背景视图
        if (_selectedView == nil) {
            _selectedView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - 3, label_width, 3)];
            _selectedView.backgroundColor = _selectedViewbgColor;
            [self addSubview:_selectedView];
        }
        // 重置视图的大小
        _selectedView.frame = CGRectMake(0, self.frame.size.height - 3, label_width, 3);
        
        // 4.设置默认选中索引位置
        _selectedIndex = 0;
        UILabel *titleLabel = titleLabels[_selectedIndex];
        titleLabel.textColor = _selectedTitleColor;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 1.获取手指位置对象
    UITouch *touch = [touches anyObject];
    // 2.获取手指点击的位置
    CGPoint touchPoint = [touch locationInView:self];
    // 3.判断当前点击视图的索引位置
    NSInteger selectedIndex = (int)touchPoint.x / (int)(self.frame.size.width / _titles.count);
    // 4.判断点击位置是否可之前一样
    if (_selectedIndex != selectedIndex) {
        self.selectedIndex = selectedIndex;
        // 5.发送事件
        [self sendActionsForControlEvents:UIControlEventValueChanged];
    }
}

- (void)setSelectedIndex:(NSInteger)selectedIndex
{
    if (_selectedIndex != selectedIndex) {
        // 设置文本的头像效果
        UILabel *oldTitleLabel = _titleLabels[_selectedIndex];
        oldTitleLabel.textColor = _titleColor;
        UILabel *newTitleLabel = _titleLabels[selectedIndex];
        newTitleLabel.textColor = _selectedTitleColor;
        _selectedIndex = selectedIndex;
        
        // 设置选中视图的位置
        // 获取滑动视图的宽度
        float width = self.frame.size.width / _titles.count;
        [UIView animateWithDuration:.22 animations:^{
            _selectedView.frame = CGRectMake( _selectedIndex * width, _selectedView.frame.origin.y, width, _selectedView.frame.size.height);
        }];

        
        
    }
}

@end
